list view in unity

56

list view in unity -

/*
The "easiest" and most elegant way I can think about is using a Vertical Layout Group

Add a new empty gameobject under your canvas
Set the desired dimensions using the RectTransform component
Attach the "Vertical Layout Group" component
In your code, for each string in your list :
Create a new GameObject :
Attach a text component to it
Fill the text attribute with your string
Set the parent of the transform to be the first empty gameobject in 1st step
Here is a piece of code I haven't tested :*/

// Drag & Drop the vertical layout group here
public UnityEngine.UI.VerticalLayoutGroup verticalLayoutGroup ;

// ... In your function
RectTransform parent = verticalLayoutGroup.GetComponent<RectTransform>() ;
for( int index = 0 ; index < stringList.Count ; ++index )
{
     GameObject g = new GameObject( stringList[index] ) ;
     UnityEngine.UI.Text t = g.AddComponent<UnityEngine.UI.Text>();
     t.addComponent<RectTransform>().setParent( parent ) ;
     t.text = stringList[index] ;
}
/*
If you need more customization, you can instantiate a prefab instead of
manually create the texts gameobjects.*/

Comments

Submit
0 Comments